package com.inxedu.os.edu.service.statistics.impl;

import com.inxedu.os.edu.dao.CourseKpointStatisticsMapper;
import com.inxedu.os.edu.dao.SystemStatisticsMapper;
import com.inxedu.os.edu.dao.UserStatisticsMapper;
import com.inxedu.os.edu.pojo.EduStatisticsDay;
import com.inxedu.os.edu.pojo.EduUserLoginLog;
import com.inxedu.os.edu.service.statistics.MainSystemStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MainSystemStatisticsImpl implements MainSystemStatistics {

    @Autowired
    private SystemStatisticsMapper systemStatisticsMapper;


    @Autowired
    private CourseKpointStatisticsMapper courseKpointMapper;

    @Autowired
    private UserStatisticsMapper userStatisticsMapper;


    @Override
    public List<EduStatisticsDay> selectMessageSomeTimesAgo(String startDate, String endDate) {
        return systemStatisticsMapper.selectLoginNumBefore(startDate,endDate);
    }

    @Override
    public List<EduStatisticsDay> selectRegisteredNumBefore(String startDate, String endDate) {
        return systemStatisticsMapper.selectRegisteredNumBefore(startDate,endDate);
    }

    @Override
    public List<EduStatisticsDay> selectVideoVieWingNumBefore(String startDate, String endDate) {
        return systemStatisticsMapper.selectVideoVieWingNumBefore(startDate,endDate);
    }

    @Override
    public List<EduStatisticsDay> selectDailyUserNumberBefore(String startDate, String endDate) {
        return systemStatisticsMapper.selectDailyUserNumberBefore(startDate,endDate);
    }

    @Override
    public List<EduStatisticsDay> selectDailyCourseNumberBefore(String startDate, String endDate) {
        return systemStatisticsMapper.selectDailyCourseNumberBefore(startDate,endDate);
    }

    @Override
    public List<EduUserLoginLog> selectUserLoginToDay() {
        return systemStatisticsMapper.selectUserLoginToDay();
    }

    @Override
    public int enerateStatistics(String time) {
        Integer courKpiontCount = courseKpointMapper.selectPlayCountNumOneDay(time);
        if (courKpiontCount==null){
            courKpiontCount=0;
        }
        Integer usercreate = userStatisticsMapper.selectCreateTimeOneDay(time);
        if (usercreate==null){
            usercreate=0;
        }
        List<EduUserLoginLog> eduUserLoginLogs = userStatisticsMapper.selectLoginTimeOneDay(time);
        Integer userloinCount = eduUserLoginLogs.size();
        Integer coourseCount = courseKpointMapper.selectCourseCountOneDay(time);
        if (coourseCount==null){
            coourseCount=0;
        }
        Integer userCount = userStatisticsMapper.selecUserCountOneDay(time);
        if (userCount==null){
            userCount=0;
        }
        EduStatisticsDay eduStatisticsDay = new EduStatisticsDay();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        eduStatisticsDay.setStatisticsTime(parse);
        eduStatisticsDay.setDailyUserNumber(userCount);
        eduStatisticsDay.setDailyCourseNumber(coourseCount);
        eduStatisticsDay.setLoginNum(userloinCount);
        eduStatisticsDay.setVideoViewingNum(courKpiontCount);
        eduStatisticsDay.setRegisteredNum(usercreate);
        int i1 = systemStatisticsMapper.selectExit(time);
        int i = 0 ;
        if (i1!=0){
            i = systemStatisticsMapper.updateStatistics(eduStatisticsDay);
        }else {
            i = systemStatisticsMapper.insertStatisticsOneDay(eduStatisticsDay);
        }
        return i;
    }

    @Override
    public int delStatistics(String statisticsTime) {
        return systemStatisticsMapper.deleteStatisticsOneDay(statisticsTime);
    }
}
